Re: [SQL] Change attribute type
От | Herouth Maoz |
---|---|
Тема | Re: [SQL] Change attribute type |
Дата | |
Msg-id | l03110705b1d77616b4c8@[147.233.159.109] обсуждение исходный текст |
Ответ на | Change attribute type (Petter Reinholdtsen <pere@hungry.com>) |
Список | pgsql-sql |
At 18:57 +0300 on 16/7/98, Petter Reinholdtsen wrote: > I have a table created with this command: > > create table test(data int4, changed timestamp); > > I want to have this table instead: > > create table test(data int4, changed datetime); > > I have lots of entries in the table, and want too keep them when I > change the date type. > > The simple method would be to change the type. Is it possible? > > The hard method requires the possibility of removing an attribute. > > 1 change name of the attribute > 'alter table test rename column changed to changedold;' > 2 create new attribute > 'alter table test add changed datetime;' > 3 insert old data in new attribute > 'update test set changed = changedold;' > 4 remove the old attribute > ? > > Any clues? Well, it seems to me the best way is to copy your table into a new one. 1. Rename the table using ALTER TABLE test RENAME to test_temp; 2. Copy the values over to a new table by: SELECT data, datetime( changed ) AS changed INTO TABLE test FROM test_temp; 3. DROP TABLE test_temp; 4. Create indices as needed on the new table. Alternatively, if your table includes DEFAULT clauses, NOT NULL clauses etc., your might want to replace step 2 above with the two steps: 2a. CREATE TABLE test ( ... new definition including DEFAULT etc. ); 2b. Copy over the values using: INSERT INTO test ( data, changed ) SELECT data, datetime( changed ) FROM test_temp; Herouth -- Herouth Maoz, Internet developer. Open University of Israel - Telem project http://telem.openu.ac.il/~herutma
В списке pgsql-sql по дате отправления: